package com.miracle.java2110.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;
import com.miracle.java2110.mapper.GuestMapper;
import com.miracle.java2110.pojo.Guest;
import com.miracle.java2110.service.GuestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("guestService")
public class GuestServiceImpl implements GuestService {

    @Autowired
    private GuestMapper guestMapper;

    @Override
    public List<Guest> findGuests() {
        return guestMapper.selectByExample(null);
    }

    @Override
    public PageVo<Guest> getPageData(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("page")+""),Integer.parseInt(params.get("limit")+""));
        List<Guest> users = guestMapper.selectByParams(params);

        PageInfo<Guest> guestPageInfo = new PageInfo<>(users);

        PageVo<Guest> guestPageVo = new PageVo<>();

        guestPageVo.setCode(0);
        guestPageVo.setMsg("查询用户分页数据");
        guestPageVo.setCount(guestPageInfo.getTotal());
        guestPageVo.setData(guestPageInfo.getList());

        return guestPageVo;
    }

    @Override
    public R updateById(Guest guest) {
        int result = guestMapper.updateByPrimaryKeySelective(guest);

        if(result==1){
            return R.ok(200,"修改成功");
        }else{
            return R.error(400,"修改失败");
        }
    }

    @Override
    public R deleteById(Integer gid) {
        int result = guestMapper.deleteByPrimaryKey(gid);

        if(result==1){
            return R.ok(200,"修改成功");
        }else{
            return R.error(400,"修改失败");
        }
    }

    @Override
    public R insertGuest(Guest guest) {
        int result = guestMapper.insertSelective(guest);
        if(result==1){
            return R.ok(200,"添加成功");
        }else{
            return R.error(400,"添加失败");
        }
    }

    @Override
    public R deleteList(Integer[] gids) {
        if (gids == null) {
            return R.error(400, "删除失败");
        } else {
            guestMapper.deleteList(gids);
            return R.ok(200, "删除成功");
        }
    }
}
